var policyText = {
    "expiration": "2020-01-01T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
    "conditions": [
    ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
    ]
};

accessid= 'LTAIm0cnDLJs1aen';
accesskey= 'Pc3M4EN1yPSdU8zny3Ln4QppC58P3G';
host = 'http://testxiang.oss-cn-shanghai.aliyuncs.com';

var time = new Date().getTime();
var policyBase64 = Base64.encode(JSON.stringify(policyText));
message = policyBase64;
var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true }) ;
var signature = Crypto.util.bytesToBase64(bytes);
var uploader = new plupload.Uploader({
	runtimes : 'html5,flash,silverlight,html4',
	browse_button : 'selectfiles',

    //runtimes : 'flash',
	container: document.getElementById('container'),
	flash_swf_url : '/plugin/oss-upload-js/lib/plupload-2.1.2/js/Moxie.swf',
	silverlight_xap_url : '/plugin/oss-upload-js/lib/plupload-2.1.2/js/Moxie.xap',

    url : host,

	multipart_params: {
		'Filename': time+'_${filename}',
        'key' : time+'_${filename}',
		'policy': policyBase64,
        'OSSAccessKeyId': accessid,
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
		'signature': signature,
	},

	init: {
		PostInit: function() {
			document.getElementById('ossfile').innerHTML = '';
			document.getElementById('postfiles').onclick = function() {
				uploader.start();
				event.preventDefault();
				var url = $(this).attr("action");
				var data = $("#picture_form").serialize();
				console.log(data);
				$.ajax({
					url: url,
					type: "POST",
					data: data,
					dataType: "json",
					success: function(res) {
						// console.log(res);
						// window.location.href = window.location.href;
						layer.msg(res.msg)
					}
				});
				return false;
			};
		},

		FilesAdded: function(up, files) {
			plupload.each(files, function(file) {
                file.name = time+'_'+file.name;
				document.getElementById('ossfile').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
				+'<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>'
				+'</div>';
				// document.getElementById('ossfile').innerHTML += '<input type="text" name="content[]"  value="" >';
			});
		},

		UploadProgress: function(up, file) {
		    // console.log(file.name);
			var d = document.getElementById(file.id);
			d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";

            var prog = d.getElementsByTagName('div')[0];
			var progBar = prog.getElementsByTagName('div')[0]
			progBar.style.width= 2*file.percent+'px';
			progBar.setAttribute('aria-valuenow', file.percent);
		},

		FileUploaded: function(up, file, info) {
            // alert(info);
			// console.log(info);
            if (info.status >= 200 || info.status < 200)
            {
                document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = 'success';
				document.getElementById('ossfile').innerHTML += '<input type="hidden" name="picName[]" id="' + file.id + '" value="'+ file.name +'" >';
				var brief = document.getElementById('annex_brief').value;
				var annex_id = document.getElementById('annex_id').value;
				var annex_rank = document.getElementById('annex_rank').value;

				var data = {annex_id:annex_id,rank:annex_rank,brief:brief,name:file.name};
				console.log(data);
				$.ajaxSetup({
					headers: {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					}
				});
				$.ajax({
					url: '/case/annex',
					type: "POST",
					data: data,
					dataType: "json",
					success: function(data) {
						console.log(data);
						layer.msg(data.msg);
					}
				});
				// 	});
				// });

            }
            else
            {
                document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
            }

		},

		Error: function(up, err) {
			document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response));
		}
	}
});

uploader.init();
